package com.funny.stock.base.util;


import com.funny.stock.base.vo.Result;
import org.springframework.http.HttpStatus;

/*
 * @author  pengl
 * @date    2020/4/9
 */
public class ResultUtil {
    public static final int CODE_SUCCESS = 0;
    public static final int CODE_FAIL = 10000;

    public static final String MSG_SUCCESS = "success";
    public static final String MSG_FAIL = "error";
    public static final String MSG_NOT_FOUND = "not found";

    public static <T> Result<T> success(T data){
        return success(data,null);
    }

//    public static <T> Result<T> success(String msg){
//        return success(null,msg);
//    }

    public static <T> Result<T> success(T data, String msg){
       return success(data,CODE_SUCCESS,msg);
    }

    public static <T> Result<T> success(T data, int code, String msg){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg != null ? msg : MSG_SUCCESS);
        return result;
    }


    public static <T> Result<T> error(int code, String msg){
        return error(CODE_FAIL,msg,null);
    }

    public static <T> Result<T> error(int code, String msg,T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg != null ? msg : MSG_FAIL);
        return result;
    }

    public static <T> Result<T> error(String msg){
        return error(CODE_FAIL,msg);
    }

    public static <T> Result<T> error(HttpStatus status){
        return error(status.value(),status.getReasonPhrase());
    }

}
